#include "hitbox.h"

declare_tick(hitbox, { })
declare_draw(hitbox, { })
declare_head(hitbox)

void add_hitbox(entity *ent, float x, float y, float width, float height) {
	hitbox *h = c_new(hitbox);
	h->position = vxy(x, y);
	h->size = vxy(width, height);
	
	add_component(ent, h);
}

int check_hitbox(entity *ent, vec point) {
	hitbox *box = get_component(ent, hitbox);
	if(!box) return 0;
	
	float xmin = box->position.x + ent->position.x;
	float ymin = box->position.y + ent->position.y;
	float xmax = xmin + box->size.x;
	float ymax = ymin + box->size.y;
	
	return
		(point.x >= xmin && point.x <= xmax) && 
		(point.y >= ymin && point.y <= ymax);
}

int check_hitbox_v(entity *ent, vec point, vec last) {
	hitbox *box = get_component(ent, hitbox);
	if(!box) return 0;
	
	float xmin = box->position.x + ent->position.x;
	float ymin = box->position.y + ent->position.y;
	float xmax = xmin + box->size.x;
	float ymax = ymin + box->size.y;
	
	int crossed_y_min = point.y > ymin && last.y < ymin;
	int crossed_y_max = point.y > ymax && last.y < ymax;
	int inside_y = (point.y >= ymin && point.y <= ymax);
	int passed_through = (crossed_y_min || crossed_y_max || inside_y);
	
	return
		(point.x >= xmin && point.x <= xmax) && 
		passed_through;
}